<?php

namespace addons\Flow\merchant\controllers;
use Yii;
use common\traits\Curd;
use common\models\base\SearchModel;
use addons\Flow\common\models\Metadata;


/**
 * 管理
 *
 * Class NoticeController
 * @package addons\RfNotice\backend\controllers
 * @author YiiFrame <21931118@qq.com>
 */
class MetadataController extends BaseController
{
    use Curd;


    public $modelClass = Metadata::class;

    /**
     * 首页
     *
     * @return string
     * @throws \yii\web\NotFoundHttpException
     */

    public function actionIndex()
    {
        $workflow_id = Yii::$app->request->get('workflow_id');
        $status_id = Yii::$app->request->get('status_id');

        $searchModel = new SearchModel([
            'model' => $this->modelClass,
            'scenario' => 'default',
            'partialMatchAttributes' => [''], // 模糊查询
            'defaultOrder' => [
                'id' => SORT_DESC
            ],
            'pageSize' => $this->pageSize
        ]);

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $dataProvider->query
            ->andWhere(['workflow_id'=>$workflow_id])
            ->andWhere(['status_id'=>$status_id])
            ->andFilterWhere([Metadata::tableName().'.merchant_id' => $this->getMerchantId()])
            ->with('workflow','status');
        return $this->render($this->action->id, [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
            'workflow_id' =>$workflow_id,
            'status_id' =>$status_id,
        ]);
    }
    public function actionCreate()
    {
        $id = Yii::$app->request->get('id', null);
        $workflow_id = Yii::$app->request->get('workflow_id');
        $status_id = Yii::$app->request->get('status_id');
        $model = $this->findModel($id);
        $model->merchant_id = Yii::$app->user->identity->merchant_id;
//        $model->value = Json::decode($model->value);

        // ajax 校验
        $this->activeFormValidate($model);
        if ($model->load(Yii::$app->request->post())) {
//            !empty($model->value) && $model->value = Json::encode($model->value);

            return $model->save()
                ? $this->redirect(['index', 'workflow_id' => $workflow_id,'status_id' => $status_id])
                : $this->message($this->getError($model), $this->redirect(['index', 'workflow_id' => $workflow_id,'status_id' => $status_id]), 'error');
        }

        return $this->renderAjax('create', [
            'model' => $model,
            'workflow_id' => $workflow_id,
            'status_id' => $status_id,
            'data' => Yii::$app->services->merchantMember->getMap(),

        ]);
    }
    public function actionEdit()
    {
        $id = Yii::$app->request->get('id', null);
        $workflow_id = Yii::$app->request->get('workflow_id');
        $status_id = Yii::$app->request->get('status_id');
        $model = $this->findModel($id);
        $model->merchant_id = Yii::$app->user->identity->merchant_id;
//        $model->value = Json::decode($model->value);

        // ajax 校验
        $this->activeFormValidate($model);
        if ($model->load(Yii::$app->request->post())) {
//            !empty($model->value) && $model->value = Json::encode($model->value);

            return $model->save()
                ? $this->redirect(['index', 'workflow_id' => $workflow_id,'status_id' => $status_id])
                : $this->message($this->getError($model), $this->redirect(['index', 'workflow_id' => $workflow_id,'status_id' => $status_id]), 'error');
        }

        return $this->renderAjax('edit', [
            'model' => $model,
            'workflow_id' => $workflow_id,
            'status_id' => $status_id,
            'data' => Yii::$app->services->merchantMember->getMap(),

        ]);
    }
    /**
     * 删除
     * @param $id
     * @return mixed
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public function actionDelete($id)
    {
        $workflow_id = Yii::$app->request->get('workflow_id');
        $status_id = Yii::$app->request->get('status_id');
        $id = Yii::$app->request->get('id');

        if ($this->findModel($id)->delete()) {
            return $this->message("删除成功", $this->redirect(['index','workflow_id'=>$workflow_id,'status_id'=>$status_id]));
        }

        return $this->message("删除失败", $this->redirect(['index','workflow_id'=>$workflow_id,'status_id'=>$status_id]), 'error');
    }

    protected function findModel($id)
    {
        /* @var $model \yii\db\ActiveRecord */
        if (empty($id) || empty(($model =  $this->modelClass::find()->andWhere(['id' => $id,'merchant_id' => $this->getMerchantId()])->one()))) {
            $model = new $this->modelClass;
            return $model->loadDefaultValues();
        }
        return $model;
    }
}